package com.bookyourtrek.model;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.bookyourtrek.util.DbConnection;
import com.bookyourtrek.util.RuntimeSettings;

public class SearchAdventureModel {

	private PreparedStatement ps = null;
	private ResultSet rs = null;
	private String sqlQuery = "";
	private Connection conn = null;
	/**
	 * function to fetch trek list according to search parameter
	 * @author ananda
	 * @param yourlocation
	 * @param adventuretype
	 * @return
	 */
	public ArrayList<SearchListPojo> gettreklist(String yourlocation,
			String adventuretype) {
		
		ArrayList<SearchListPojo> searchlistpojo = new ArrayList<SearchListPojo>();
		sqlQuery = "SELECT trekkingid, trektitle, location, date FROM bookyourtrek.trekking"
				+ " WHERE location = ? AND status = " + RuntimeSettings.ADVENTURESTATUSACTIVE.toString() 
				+ " ORDER BY date DESC";
		conn = DbConnection.getConnection();
		//System.out.println(yourlocation);
		try {
			ps = conn.prepareStatement(sqlQuery);
			ps.setString(1, yourlocation);
			rs = ps.executeQuery();
			while (rs.next()) {
				//System.out.println("here");
				SearchListPojo slp = new SearchListPojo();
				slp.setAdventureid(BigInteger.valueOf(rs.getLong(1)));
				slp.setAdventuretitle(rs.getString(2));
				slp.setAdventurelocation(rs.getString(3));
				SimpleDateFormat toFormat = new SimpleDateFormat("d MMM, yyyy");
				slp.setAdventuredate(toFormat.format(rs.getDate(4)));
				slp.setAdventuretype(RuntimeSettings.EVENTTREKKING.toString());
				searchlistpojo.add(slp);
			}		
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
		}
		return searchlistpojo;
	}
	
	
	/**
	 * get all pending treks
	 * @author ananda
	 * @return ArrayList
	 */
	public ArrayList<SearchListPojo> getpendingtreks() {
		ArrayList<SearchListPojo> searchlistpojo = new ArrayList<SearchListPojo>();
		sqlQuery = "SELECT trekkingid, trektitle, location, date FROM bookyourtrek.trekking"
				+ " WHERE status = ?"  
				+ " ORDER BY date";
		conn = DbConnection.getConnection();
		//System.out.println(yourlocation);
		try {
			ps = conn.prepareStatement(sqlQuery);
			ps.setInt(1, RuntimeSettings.ADVENTURESTATUSPENDING);
			rs = ps.executeQuery();
			while (rs.next()) {
				//System.out.println("here");
				SearchListPojo slp = new SearchListPojo();
				slp.setAdventureid(BigInteger.valueOf(rs.getLong(1)));
				slp.setAdventuretitle(rs.getString(2));
				slp.setAdventurelocation(rs.getString(3));
				SimpleDateFormat toFormat = new SimpleDateFormat("d MMM, yyyy");
				slp.setAdventuredate(toFormat.format(rs.getDate(4)));
				slp.setAdventuretype(RuntimeSettings.EVENTTREKKING.toString());
				searchlistpojo.add(slp);
			}		
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
		}
		return searchlistpojo;
	}
}
